/*
* pps_bom
*/
package com.pitop.assembly.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.account.entity.Account;
import com.pitop.assembly.entity.Assembly;
import com.pitop.assembly.service.AssemblySVImpl;
import com.pitop.base.ctrl.BaseCtrl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 组件 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/assembly")
@Slf4j
@Api(value = "组件控制器", description = "组件控制器")
public class AssemblyCtrl  extends BaseCtrl{

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private AssemblySVImpl assemblySVImpl;



    /**
     * 查询Assembly一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Assembly一个详情信息", notes = "查询Assembly一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Assembly assembly = assemblySVImpl.load(id, code);
        log.info(JSON.toJSONString(assembly));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }

    /**
     * 查询Assembly一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Assembly一个详情信息", notes = "查询Assembly一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Assembly assembly = assemblySVImpl.get(id, code);
        log.info(JSON.toJSONString(assembly));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }


    /**
     * 根据条件id查询Assembly一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Assembly一个详情信息", notes = "查询Assembly一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Assembly assembly = assemblySVImpl.loadById(id);
        log.info(JSON.toJSONString(assembly));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }

    /**
     * 根据条件code查询Assembly一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Assembly一个详情信息", notes = "查询Assembly一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Assembly assembly = assemblySVImpl.loadByCode(code);
        log.info(JSON.toJSONString(assembly));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }

    /**
     * 根据条件id查询Assembly一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Assembly一个详情信息", notes = "查询Assembly一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Assembly assembly = assemblySVImpl.getById(id);
        log.info(JSON.toJSONString(assembly));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }

    /**
     * 根据条件code查询Assembly一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Assembly一个详情信息", notes = "查询Assembly一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public BeanRet getByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Assembly assembly = assemblySVImpl.getByCode(code);
        log.info(JSON.toJSONString(assembly));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }

    /**
     * 查询Assembly信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Assembly信息集合", notes = "查询Assembly信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件代码", paramType = "query"),
            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Assembly assembly, @ApiIgnore Page<Assembly> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Assembly> assemblys = assemblySVImpl.list(assembly, page.getCurPage(), page.getPageSize());
        int total = assemblySVImpl.count(assembly);
        page.setTotalRow(total);
        page.setVoList(assemblys);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 根据bom编码查询组件信息
     *
     * @return 分页对象
     */
    @ApiOperation(value = "根据bom编码查询组件信息", notes = "根据bom编码查询组件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/listForBom")
    @ResponseBody
    public BeanRet listForBom(String bomCode, String name, @ApiIgnore Page<Assembly> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Assembly> assemblys = assemblySVImpl.listForBom(bomCode, name, page.getCurPage(), page.getPageSize());
        int total = assemblySVImpl.countForBom(bomCode, name);
        page.setTotalRow(total);
        page.setVoList(assemblys);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询Assembly信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Assembly信息集合", notes = "查询Assembly信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<Assembly> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Assembly> assemblys = assemblySVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = assemblySVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(assemblys);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建Assembly
     * 1.参数合法性验证
     * ****1.1参数是否为空
     * ****1.2组件名称是否已经存在，组件代码是否已经存在
     * ****1.3组件类型编码是否有效
     * ****1.4组件上级编码是否正确
     * 2.上级编码是否存在，如果不存在，设置组件级别为1级；如果存在，上级组件级别加1
     * 3.持久化组件数据
     * 4.持久化构成组件的物料表
     * 5.持久化组件日志
     * 6.图纸信息存在，持久化组件信息
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Assembly", notes = "创建Assembly")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryCode", value = "组件类型编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "命名", paramType = "query", required = true),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件代码", paramType = "query"),
            @ApiImplicitParam(name = "designFileUrls", value = "图纸地址：多个地址使用逗号分开", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "组件版本", paramType = "query"),
            @ApiImplicitParam(name = "materialCodes", value = "所有物料的编码", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Assembly assembly, HttpServletRequest request) {
        if (StringUtils.isEmpty(assembly.getCategoryCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(assembly.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

       Account account = this.loadAccount(request);
        assemblySVImpl.save(assembly, account.getAccount(), account.getCode());
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }

    /**
     * 修改Assembly
     * * 1.参数合法性验证
     * ****1.1 参数是否为空
     * ****1.2 根据组件编码查询组件
     * ****1.3 组件名称是否已经存在，组件代码是否已经存在
     * ****1.4 组件类型编码是否有效
     * 2.1 组件上级编码是否正确
     * 2.2上级编码是否存在，如果不存在，设置组件级别为1级；如果存在，上级组件级别加1
     * 3.持久化组件数据：修改
     * 4.持久化构成组件的物料表，先删除再添加
     * 5.持久化组件日志
     * 6.图纸信息存在，持久化组件信息：删除重新添加
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Assembly", notes = "修改Assembly")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "categoryCode", value = "组件类型编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "子级级别", paramType = "query"),
            @ApiImplicitParam(name = "assemblyCode", value = "组件代码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "命名", paramType = "query"),
            @ApiImplicitParam(name = "designFileUrls", value = "图纸地址", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "组件版本", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Assembly assembly, HttpServletRequest request) {
        if (StringUtils.isEmpty(assembly.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(assembly.getCategoryCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(assembly.getAssemblyCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(assembly.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Account account = this.loadAccount(request);
        assemblySVImpl.modify(assembly, account.getAccount(), account.getCode());
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, assembly);
    }

    /**
     * 添加组件物料
     *
     * @param code          组件编码
     * @param materialCodes 状态
     * @return
     */
    @ApiOperation(value = "添加组件物料", notes = "添加组件物料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "materialCodes", value = "物料编码", paramType = "query", required = true)
    })
    @PostMapping("/addMaterial")
    @ResponseBody
    public BeanRet addMaterial(String code, String materialCodes) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(materialCodes)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblySVImpl.addMaterial(code, materialCodes);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }


    /**
     * 复制组件
     *
     * @param code          组件编码
     * @return  BeanRet
     */
    @ApiOperation(value = "复制组件", notes = "复制组件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
    })
    @GetMapping("/copy")
    @ResponseBody
    public BeanRet copy(String code, HttpServletRequest request) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Account account = this.loadAccount(request);
        assemblySVImpl.copy(code, account.getAccount(), account.getCode());
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }


    /**
     * 修改组件状态
     *
     * @param code  组件编码
     * @param state 状态
     * @return
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1032):Develop(开发) Stable(稳定) Stop(停用)", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(state)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblySVImpl.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 根据主键删除Assembly
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Assembly", notes = "删除Assembly")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        assemblySVImpl.delete(id, code);
        return BeanRet.create(true, "删除Assembly成功");
    }

}
